package edu.taru.util;

import edu.taru.controller.UserController;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class CheckedTag extends SimpleTagSupport {
/*
    *//**
     * 要遍历的数据：Map,List,Object[],Object;;;改为下面从session中取
     *//*
    private Object items;*/

    public static HttpSession session;

    /**
     * 当前值
     */
    private String value;

    public void setValue(String value) {
        this.value = value;
    }

    public void doTag() throws IOException {
        Object items = session.getAttribute("codes");
        //获取输出流
        JspWriter out = this.getJspContext().getOut();
        //定义一个标志位
        boolean flag = false;
        try {
            if(items instanceof Collection){
                //items为Map、List类型
                Collection collection = (Collection) items;
                Iterator it = collection.iterator();
                while(it.hasNext()){
                    String next = (String) it.next();
                    if(next.equals(value)){
                        out.write("inline");
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    out.write("none");
                }
            } else if(items instanceof Object[]){
                //items为Object[]数组类型
                Object[] array = (Object[]) items;
                for(Object o : array){
                    if(o.equals(value)){
                        out.write("inline");
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    out.write("none");
                }
            } else{
                //items为一个简单数据类型
                if(value.equals(items)){
                    out.write("inline");
                    flag = true;
                }else{
                    out.write("none");
                }
            }
        } catch (Exception e) {
            //如果有异常输出空
            out.write("none");
        }

    }

}
